package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.FreeFunction;

public class FreeFuncService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public FreeFuncService(){
	}
	
	public FreeFuncService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<FreeFunction> getAllFreeFunction(){
		List<FreeFunction> obj = new ArrayList<FreeFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select * from " + DatabaseSchema.FreeFunction.SCHEMA_NAME;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ff_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.FreeFunction.ID));
			String ff_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.FreeFunction.Name));
			
			obj.add(new FreeFunction(ff_id, ff_name));
		}
		cursor.close();
		return obj;
	}
	
	public List<FreeFunction> getFreeFunctionForFuncPanel(){
		List<FreeFunction> obj = new ArrayList<FreeFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select * from " + DatabaseSchema.FreeFunction.SCHEMA_NAME + " where ff_id > 9";
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		obj.add(new FreeFunction(0, "{Empty Space}"));
		while (cursor.moveToNext()) {
			int ff_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.FreeFunction.ID));
			String ff_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.FreeFunction.Name));
			
			obj.add(new FreeFunction(ff_id, ff_name));
		}
		cursor.close();
		return obj;
	}
	
	public List<FreeFunction> getActiveFreeFunction(){
		List<FreeFunction> obj = new ArrayList<FreeFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select * from " + DatabaseSchema.FreeFunction.SCHEMA_NAME + " where " + DatabaseSchema.FreeFunction.APPR_FL + "= '1' order by ff_name" ;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ff_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.FreeFunction.ID));
			String ff_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.FreeFunction.Name));
			
			obj.add(new FreeFunction(ff_id, ff_name));
		}
		cursor.close();
		return obj;
	}

	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
